package ar.republicagroup.gestion.dao.jpa;

import ar.republicagroup.gestion.dao.UsuarioDAO;
import ar.republicagroup.gestion.entities.Usuario;
import ar.republicagroup.gestion.util.JPAUtil;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.swing.DefaultComboBoxModel;
import org.apache.log4j.Logger;

public class UsuarioDAOImplJPA implements UsuarioDAO {
    
        private static Logger log   = Logger.getLogger(UsuarioDAOImplJPA.class);
        private EntityManager		 em;

   
        
        
    public UsuarioDAOImplJPA() {
            em = JPAUtil.getEntityManager();
     }
        
    @Override
    public List<Usuario> findEntities() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String insert(Usuario beanUsuario) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String update(Usuario beanUsuario) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Usuario get(Integer idusuario) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String delete(Integer idusuario) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public DefaultComboBoxModel combo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Usuario validarUsuario(String usuario, String contrasena) {
                log.info("Validando usuario..."); 
       try {
               if(!em.isOpen()){ em = JPAUtil.getEntityManager(); }
               
                Query query = em.createQuery("SELECT u FROM Usuario u WHERE u.login = :login AND u.clave = :clave");
                query.setParameter("login", usuario);
                query.setParameter("clave", contrasena);
                return (Usuario)query.getSingleResult();
            }catch(NoResultException ex){
                log.error(ex.getMessage()); 
                return null ;
            }
 
    }
    
    
    
    
}
